<?php

namespace App\Modules\Health\Http\Controllers\Admin;

use App\Modules\Health\Repositories\HealthDiseaseDiscern;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class HealthDiseaseDiscernController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HealthDiseaseDiscern(['class']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('class_id');
            $grid->column('class.name', '分类名称');
            $grid->column('name');
            $grid->column('img')->image(' ', 50, 50);
            $grid->column('is_show')->switch();
            $grid->column('created_at');
            $grid->column('updated_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->like('name')->width(2);
                $filter->equal('class_id', '分类')->select(\App\Modules\Health\Models\HealthDiseaseDiscernClass::where('is_show', 1)->pluck('name', 'id')->all())->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HealthDiseaseDiscern(), function (Show $show) {
            $show->field('id');
            $show->field('class_id');
            $show->field('name');
            $show->field('img');
            $show->field('text');
            $show->field('treat_text');
            $show->field('is_show');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HealthDiseaseDiscern(), function (Form $form) {
            $form->display('id');
            $form->select('class_id', '分类')->options(\App\Modules\Health\Models\HealthDiseaseDiscernClass::where('is_show', 1)->pluck('name', 'id')->all())->required();
            $form->text('name')->required();
            $form->image('img')->help('建议尺寸: 750X414PX')->saving(function ($v) {
                return (string)$v;
            })->autoUpload()->uniqueName()->saveFullUrl()->required();
            $form->ueditor('text');
            $form->ueditor('treat_text');
            $form->file('audio_file')->chunked()->chunkSize(1024 * 5)->maxSize(1024 * 2 * 1000)->autoUpload()->uniqueName()->saveFullUrl();
            $form->file('video_file')->chunked()->chunkSize(1024 * 5)->maxSize(1024 * 2 * 1000)->autoUpload()->uniqueName()->saveFullUrl();
            $form->radio('is_show')->options(['隐藏', '显示'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
